package com.oocl.printFormat;

import java.util.Set;

import com.oocl.db.SetFactory;
import com.oocl.modle.Customer;

public class PrintFormat {
	public void complete(){
		System.out.println("Database initialize complete......");
	}
	
	public void printById(){
		System.out.println("Order by ID:");
		printAttribute();
		Set<Customer> set = SetFactory.getInstance();
		for(Customer c : set){
			System.out.println(c.toString());
		}
		System.out.println();
	}
	
	public String printByBirth(){
		Set<Customer> set = SetFactory.getInstance();
		Customer[] cs = new Customer[set.size()];
		int index = 0;
		for(Customer c: set){
			cs[index] = c;
			index++;
		}
		
		for(int i=0;i<cs.length-1;i++){
			for (int j = 0; j < cs.length-i-1; j++) {
				if(cs[j].compareTo(cs[j+1])>0){
					Customer temp = cs[j];
					cs[j] = cs[j+1];
					cs[j+1] = temp;
				}
			}	
		}
		
		StringBuilder sb = new StringBuilder();
		sb.append("Order by Birthday:\r\n");
		sb.append("ID\tNAME\t\tE-MAIL\t\tSEX\tBIRTHDAY\r\n");
		for(Customer c : cs){
			sb.append(c.toString() + "\r\n");
		}
		System.out.println(sb.toString());
		System.out.println();
		return sb.toString();
	}
		
	public String printXML(){
		Set<Customer> set = SetFactory.getInstance();
		Customer[] cs = new Customer[set.size()];
		int index = 0;
		for(Customer c: set){
			cs[index] = c;
			index++;
		}
		
		for(int i=0;i<cs.length-1;i++){
			for (int j = 0; j < cs.length-i-1; j++) {
				if(cs[j].compareTo(cs[j+1])>0){
					Customer temp = cs[j];
					cs[j] = cs[j+1];
					cs[j+1] = temp;
				}
			}	
		}
		
		StringBuilder builder = new StringBuilder("");
		for (int i = 0; i < cs.length; i++) {
			builder.append("<Customer id=\""+cs[i].getId()+"\">"+"\r\n");
			builder.append("\t<name>"+cs[i].getName()+"</name>" +"\r\n");
			builder.append("\t<birthday>"+cs[i].getBirth()+"</birthday>"+"\r\n");
			builder.append("\t<sex> "+cs[i].getSex()+"</sex>"+"\r\n");
			builder.append("\t<email>"+cs[i].getEmail()+"</email>"+"\r\n");
			builder.append("<Customer>"+"\r\n");
		}
		String result = builder.toString();
		System.out.println(result);
		System.out.println();
		return result;
	}
	
	public void printAttribute(){
		System.out.println("ID\tNAME\t\tE-MAIL\t\tSEX\tBIRTHDAY");
	}
	
	public void printCommandError(){
		System.out.println("Command Error!");
	}
	public String printWelcome(){
		return "Welcome to ims \r\n";
	}
	
}
